package com.ym.b2b.complaint.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.commons.log.CusLog;
import com.ym.b2b.complaint.service.EmBComplaintService;
import com.ym.b2b.complaint.sqlbean.EmBComplaint;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;

@Controller
@RequestMapping("/complaint")
public class ManagerComplaintController extends BaseController {

    @Autowired
    private EmBComplaintService emBComplaintService;

    // 列表查询
    @RequestMapping("searchPageEmBComplaint.do")
    @ResponseBody
    public Result searchPageEmBComplaint(EmBComplaint emBComplaint, Page<EmBComplaint> page, HttpServletRequest req) {
        Result result = new Result();
        result = emBComplaintService.searchPageEmBComplaint(emBComplaint, page);
        return result;
    }

    // 导出
    @RequestMapping(value = "searchPageEmBComplaintExcel.do")
    @ResponseBody
    public Result searchPageEmBComplaintExcel(EmBComplaint emBComplaint, Page<EmBComplaint> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            ret = emBComplaintService.searchPageEmBComplaint(emBComplaint, page);
            ExcelUtil<EmBComplaint> excelUtil = new ExcelUtil<EmBComplaint>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("complaintListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    // 获取单条记录内容
    @RequestMapping("searchEmBComplaint.do")
    @ResponseBody
    public EmBComplaint searchEmBComplaint(EmBComplaint emBComplaint, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + "获取单条记录内容: " + JSON.toJSONString(emBComplaint), "USER", 2);
        EmBComplaint result = new EmBComplaint();
        result = emBComplaintService.searchEmBComplaint(emBComplaint);
        return result;
    }

    // 删除记录内容
    @RequestMapping("deleteEmBComplaint.do")
    @ResponseBody
    public Result deleteEmBNews(EmBComplaint emBComplaint, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + "删除投诉内容emBComplaint: " + JSON.toJSONString(emBComplaint), "USER", 2);
        Result result = new Result();
        try {
            emBComplaintService.deleteEmBComplaint(emBComplaint.getComplaintId());
        } catch (Exception e) {
            // TODO: handle exception
            result.setCode("100");
            return result;
        }
        result.setCode("200");
        return result;
    }

    // 删除多条记录内容
    @RequestMapping("deleteMultipleEmBComplaint.do")
    @ResponseBody
    public Result deleteMultipleEmBComplaint(String complaintIdList, HttpServletRequest req) {
        Result result = new Result();
        try {
            emBComplaintService.deleteMultipleEmBComplaint(complaintIdList);
        } catch (Exception e) {
            // TODO: handle exception
            result.setCode("100");
            return result;
        }
        result.setCode("200");
        return result;
    }

    // 更新记录
    @RequestMapping("updateEmBComplaint.do")
    @ResponseBody
    public Result updateEmBNews(EmBComplaint emBComplaint, HttpServletRequest req) throws DataAccessException {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + "更新客户反馈回复: " + JSON.toJSONString(emBComplaint), "USER", 2);
        Result result = new Result();
        result = emBComplaintService.updateEmBComplaint(emBComplaint);
        return result;
    }

    // 批量更新记录
    @RequestMapping("updateEmBComplaintBat.do")
    @ResponseBody
    public Result updateEmBNewsBat(EmBComplaint emBComplaint, HttpServletRequest req) throws DataAccessException {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + "批量更新记录客户反馈回复: " + JSON.toJSONString(emBComplaint), "USER", 2);
        Result result = new Result();
        result = emBComplaintService.updateEmBComplaintBat(emBComplaint);
        return result;
    }

}
